
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>行为 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="gen-stage.html" />
    
    
    <link rel="prev" href="typespec.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >行为</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x6211;&#x4EEC;&#x5728;&#x524D;&#x9762;&#x7684;&#x8BFE;&#x7A0B;&#x5B66;&#x4E60;&#x4E86;&#x7C7B;&#x578B;&#x548C; specification&#x3002;&#x90A3;&#x4E48;&#xFF0C;&#x8FD9;&#x4E00;&#x7AE0;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60;&#x5982;&#x4F55;&#x5F15;&#x7528;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x6765;&#x5B9E;&#x73B0;&#x90A3;&#x4E9B; specification&#x3002;&#x5728; Elixir &#x91CC;&#xFF0C;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x88AB;&#x79F0;&#x4E4B;&#x4E3A;&#x884C;&#x4E3A;&#x3002;  </p>
<h2 id="&#x4F7F;&#x7528;&#x573A;&#x666F;">&#x4F7F;&#x7528;&#x573A;&#x666F;</h2>
<p>&#x6709;&#x65F6;&#x5019;&#xFF0C;&#x4F60;&#x5E0C;&#x671B;&#x4E00;&#x4E9B;&#x6A21;&#x5757;&#x80FD;&#x591F;&#x5206;&#x4EAB;&#x4E00;&#x4E2A;&#x5F00;&#x653E;&#x7684; API &#x63A5;&#x53E3;&#xFF0C;&#x5728; Elixir &#x91CC;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x5C31;&#x662F;&#x884C;&#x4E3A;&#x3002;&#x884C;&#x4E3A;&#x627F;&#x62C5;&#x4E86;&#x4E24;&#x5927;&#x89D2;&#x8272;&#xFF1A;  </p>
<ul>
<li>&#x5B9A;&#x4E49;&#x4E00;&#x7EC4;&#x9700;&#x8981;&#x88AB;&#x5B9E;&#x73B0;&#x7684;&#x51FD;&#x6570;  </li>
<li>&#x68C0;&#x67E5;&#x8FD9;&#x7EC4;&#x51FD;&#x6570;&#x662F;&#x5426;&#x771F;&#x7684;&#x88AB;&#x5B9E;&#x73B0;  </li>
</ul>
<p>Elixir &#x9884;&#x5B9A;&#x4E49;&#x4E86;&#x597D;&#x4E00;&#x4E9B;&#x884C;&#x4E3A;&#xFF0C;&#x4F8B;&#x5982; <code>GenServer</code>&#xFF0C;&#x4F46;&#x662F;&#xFF0C;&#x672C;&#x6B21;&#x8BFE;&#x7A0B;&#x6211;&#x4EEC;&#x7684;&#x91CD;&#x70B9;&#x662F;&#x521B;&#x5EFA;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x884C;&#x4E3A;&#x3002;  </p>
<h2 id="&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x884C;&#x4E3A;">&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x884C;&#x4E3A;</h2>
<p>&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x7406;&#x89E3;&#x884C;&#x4E3A;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x4E3A; worker &#x6A21;&#x5757;&#x6765;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x4E00;&#x7EC4;&#x884C;&#x4E3A;&#x3002;&#x8FD9;&#x4E9B; workers &#x90FD;&#x5E94;&#x8BE5;&#x5B9E;&#x73B0;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#xFF1A;<code>init/1</code> &#x548C; <code>perform/2</code>&#x3002;  </p>
<p>&#x5B9E;&#x73B0;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x4F7F;&#x7528;&#x548C; <code>@spec</code> &#x8BED;&#x6CD5;&#x7C7B;&#x4F3C;&#x7684; <code>@callback</code> &#x6CE8;&#x89E3;&#x3002;&#x8FD9;&#x4E2A;&#x6CE8;&#x89E3;&#x5B9A;&#x4E49;&#x4E86; <strong>required</strong> &#x51FD;&#x6570;&#xFF1B;&#x5BF9;&#x4E8E;&#x5B8F;&#x6765;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>@macrocallback</code>&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B; <code>init/1</code> &#x548C; <code>perform/2</code> &#x5728; workers &#x91CC;&#x600E;&#x4E48;&#x5B9A;&#x4E49;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Worker <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@callback</span> init(state <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term) <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> {<span class="hljs-symbol">:ok</span>, new_state <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term} | {<span class="hljs-symbol">:error</span>, reason <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term}
  <span class="hljs-variable">@callback</span> perform(args <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term, state <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term) <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span>
              {<span class="hljs-symbol">:ok</span>, result <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term, new_state <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term}
              | {<span class="hljs-symbol">:error</span>, reason <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term, new_state <span class="hljs-symbol">:</span><span class="hljs-symbol">:</span> term}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86; <code>init/1</code> &#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x63A5;&#x6536;&#x4EFB;&#x4F55;&#x53C2;&#x6570;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; &#x503C;&#x4E3A; <code>{:ok, state}</code> &#x6216; <code>{:error, reason}</code> &#x7684; Tuple&#xFF08;&#x5143;&#x7EC4;&#xFF09;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x5178;&#x578B;&#x7684;&#x521D;&#x59CB;&#x5316;&#x65B9;&#x5F0F;&#x3002;<code>perform/2</code> &#x51FD;&#x6570;&#x5219;&#x63A5;&#x6536;&#x4E00;&#x4E9B;&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x53CA;&#x521D;&#x59CB;&#x5316;&#x540E;&#x7684;&#x72B6;&#x6001;&#x3002;<code>perform/2</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x5219;&#x662F; <code>{:ok, result, state}</code> &#x6216;&#x8005; <code>{:error, reason, state}</code>&#xFF0C;&#x5F88;&#x50CF; GenServers&#x3002;  </p>
<h2 id="&#x5B9E;&#x73B0;&#x4F7F;&#x7528;&#x884C;&#x4E3A;">&#x5B9E;&#x73B0;&#x4F7F;&#x7528;&#x884C;&#x4E3A;</h2>
<p>&#x65E2;&#x7136;&#x5DF2;&#x7ECF;&#x5B9A;&#x4E49;&#x597D;&#x6211;&#x4EEC;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x4E0B;&#x4E00;&#x6B65;&#x6211;&#x4EEC;&#x4E5F;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E9B;&#x62E5;&#x6709;&#x540C;&#x6837;&#x7684;&#x5F00;&#x653E;&#x7684; API &#x63A5;&#x53E3;&#x7684;&#x6A21;&#x5757;&#x4E86;&#x3002;&#x5728;&#x6A21;&#x5757;&#x4E2D;&#x6DFB;&#x52A0;&#x884C;&#x4E3A;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>@behaviour</code> &#x5C5E;&#x6027;&#x3002;  </p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;&#x4E86;&#x8FD9;&#x4E9B;&#x65B0;&#x884C;&#x4E3A;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x5B83;&#x7684;&#x529F;&#x80FD;&#x662F;&#x53EF;&#x4EE5;&#x628A;&#x8FDC;&#x7AEF;&#x7684;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E0B;&#x8F7D;&#x5230;&#x672C;&#x5730;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Downloader <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@behaviour</span> Example.Worker

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, opts}

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform</span></span>(url, opts) <span class="hljs-keyword">do</span>
    url
    |&gt; HTTPoison.get!()
    |&gt; Map.fetch(<span class="hljs-symbol">:body</span>)
    |&gt; write_file(opts[<span class="hljs-symbol">:path</span>])
    |&gt; respond(opts)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">write_file</span></span>(<span class="hljs-symbol">:error</span>, <span class="hljs-number">_</span>), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, <span class="hljs-symbol">:missing_body</span>}

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">write_file</span></span>({<span class="hljs-symbol">:ok</span>, contents}, path) <span class="hljs-keyword">do</span>
    path
    |&gt; Path.expand()
    |&gt; File.write(contents)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>(<span class="hljs-symbol">:ok</span>, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, opts[<span class="hljs-symbol">:path</span>], opts}
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>({<span class="hljs-symbol">:error</span>, reason}, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, reason, opts}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6216;&#x8005;&#xFF0C;&#x4E00;&#x4E2A;&#x80FD;&#x538B;&#x7F29;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x7684;&#x6587;&#x4EF6;&#x7684; worker&#xFF1F;&#x8FD9;&#x4E5F;&#x662F;&#x53EF;&#x884C;&#x7684;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Compressor <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@behaviour</span> Example.Worker

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">init</span></span>(opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, opts}

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform</span></span>(payload, opts) <span class="hljs-keyword">do</span>
    payload
    |&gt; compress
    |&gt; respond(opts)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">compress</span></span>({name, files}), <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:zip</span>.create(name, files)

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>({<span class="hljs-symbol">:ok</span>, path}, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, path, opts}
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>({<span class="hljs-symbol">:error</span>, reason}, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, reason, opts}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x867D;&#x7136;&#x5B83;&#x4EEC;&#x7684;&#x529F;&#x80FD;&#x5F88;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E9B;&#x5F00;&#x653E;&#x7684; API &#x63A5;&#x53E3;&#x5374;&#x662F;&#x76F8;&#x540C;&#x7684;&#x3002;&#x4EFB;&#x4F55;&#x4F7F;&#x7528;&#x4E86;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x90FD;&#x80FD;&#x975E;&#x5E38;&#x786E;&#x5B9A;&#x8FD9;&#x4E9B;&#x6A21;&#x5757;&#x7684;&#x884C;&#x4E3A;&#x548C;&#x8FD4;&#x56DE;&#x548C;&#x671F;&#x671B;&#x662F;&#x4E00;&#x81F4;&#x7684;&#x3002;&#x8FD9;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4EFB;&#x4F55;&#x6570;&#x91CF;&#x7684; worker&#xFF0C;&#x5373;&#x4FBF;&#x5404;&#x81EA;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x4F46;&#x662F;&#x4ECD;&#x7136;&#x7B26;&#x5408;&#x4E00;&#x81F4;&#x7684;&#x5F00;&#x653E; API &#x63A5;&#x53E3;&#x3002;  </p>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;&#x67D0;&#x4E2A;&#x884C;&#x4E3A;&#xFF0C;&#x4F46;&#x662F;&#x6CA1;&#x6709;&#x628A;&#x6240;&#x6709;&#x7684;&#x51FD;&#x6570;&#x90FD;&#x5B9E;&#x73B0;&#xFF0C;&#x4EE3;&#x7801;&#x5728;&#x7F16;&#x8BD1;&#x671F;&#x5C31;&#x4F1A;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x8B66;&#x544A;&#x3002;&#x6211;&#x4EEC;&#x9A6C;&#x4E0A;&#x4FEE;&#x6539;&#x4E00;&#x4E0B; <code>Example.Compressor</code>&#xFF0C;&#x5220;&#x9664; <code>init/1</code> &#x51FD;&#x6570;&#xFF0C;&#x770B;&#x770B;&#x60C5;&#x51B5;&#x4F1A;&#x662F;&#x600E;&#x4E48;&#x6837;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span>.Compressor <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@behaviour</span> Example.Worker

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">perform</span></span>(payload, opts) <span class="hljs-keyword">do</span>
    payload
    |&gt; compress
    |&gt; respond(opts)
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">compress</span></span>({name, files}), <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:zip</span>.create(name, files)

  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>({<span class="hljs-symbol">:ok</span>, path}, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:ok</span>, path, opts}
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">respond</span></span>({<span class="hljs-symbol">:error</span>, reason}, opts), <span class="hljs-symbol">do:</span> {<span class="hljs-symbol">:error</span>, reason, opts}
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x7F16;&#x8BD1;&#x4EE3;&#x7801;&#x7684;&#x8BDD;&#xFF0C;&#x5E94;&#x8BE5;&#x4F1A;&#x51FA;&#x73B0;&#x8FD9;&#x6837;&#x7684;&#x8B66;&#x544A;&#xFF1A;  </p>
<pre><code class="lang-shell">lib/example/compressor.ex:1: warning: undefined behaviour function init/1 (for behaviour Example.Worker)
Compiled lib/example/compressor.ex
</code></pre>
<p>&#x5927;&#x529F;&#x544A;&#x6210;&#xFF01;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x6709;&#x80FD;&#x529B;&#x5236;&#x5B9A;&#x548C;&#x5206;&#x4EAB;&#x884C;&#x4E3A;&#x7ED9;&#x5176;&#x4ED6;&#x4EBA;&#x4E86;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="typespec.html" class="navigation navigation-prev " aria-label="Previous page: Specifications and types">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="gen-stage.html" class="navigation navigation-next " aria-label="Next page: GenStage">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"行为","level":"2.11","depth":1,"next":{"title":"GenStage","level":"2.12","depth":1,"path":"lessons/advanced/gen-stage.md","ref":"lessons/advanced/gen-stage.md","articles":[]},"previous":{"title":"Specifications and types","level":"2.10","depth":1,"path":"lessons/advanced/typespec.md","ref":"lessons/advanced/typespec.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/behaviours.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

